<?php
namespace App\Domain;
use App\Model\Login as Model_Login;
use App\Model\Live as Model_Live;

class Login {

    public function userLogin($user_login,$user_pass,$source,$country_code) {
        $rs = array();
        
        $configpri=\App\getConfigPri();

        $where=[
            'user_login = ?'=>$user_login
        ];
        $model = new Model_Login();
        $info = $model->userLogin($where);
        
        if(!$info){
			//注册奖励
			$configpri=\App\getConfigPri();
			$reg_reward=$configpri['reg_reward'];
			
            /* 注册 */
            $nickname=\PhalApi\T('User').rand(100000,999999);
			$data=array(
                'country_code'=>$country_code,
				'user_login' => $user_login,
				'user_nickname' =>$nickname,
				"source"=>$source,
				"mobile"=>$user_login,
				"coin"=>$reg_reward,
				"isvideo"=>1,
				"video_value"=>500,
			);
            
            $info = $model->userReg($data);

            if($reg_reward){
            	//添加钻石记录
	            $insert=array(
	                'type'=>1,
	                'action'=>8, //注册奖励
	                'uid'=>$info['id'],
	                'touid'=>$info['id'],
	                'actionid'=>0,
	                'nums'=>0,
	                'totalcoin'=>$reg_reward,
	                'addtime'=>time()
	            );

	            \App\addCoinRecord($insert);
            }
  

        }
        
        
		if($info['user_status']=='0'){
			return 1002;
        }
		if($info['user_status']=='3'){
			return 1003;
        }
		unset($info['user_status']);
		
		$info['isreg']='0';
		$info['issexrecommend']='0';
        
		if($info['sex']==0){
			$info['isreg']='1';
			$info['issexrecommend']='1';
		}
        
        if(!$configpri['agent_switch']){
            $info['isreg']='0';
        }
        
        unset($info['last_login_time']);
 
		$info['level']=\App\getLevel($info['consumption']);
		$info['level_anchor']=\App\getLevelanchor($info['votestotal']);
		/* $info['level_anchor']=getLevelanchor($info['goodnums'],$info['badnums']); */

		$info['avatar']=\App\get_upload_path($info['avatar']);
		$info['avatar_thumb']=\App\get_upload_path($info['avatar_thumb']);

        $userinfo=$info;
		unset($userinfo['issexrecommend']);
        unset($userinfo['isreg']);
        unset($userinfo['user_status']);
        unset($userinfo['coin']);
        unset($userinfo['login_type']);
        
        \App\hMSet('userinfo_'.$userinfo['id'],$userinfo);
        
        
        $token=md5(md5($info['id'].$user_login.time()));
		$info['token']=$token;
		$model->updateToken($info['id'],$token);
        
        $usersig=\App\setSig($info['id']);
		$info['usersig']=$usersig;
        
        unset($info['goodnums']);
        unset($info['badnums']);
        
        return $info;

    }
    
    public function userLoginByDevice($user_login,$user_pass,$source,$country_code) {
        $rs = array();
        
        $configpri=\App\getConfigPri();

        $where=[
            'device_code = ?'=>$user_login
        ];
        $model = new Model_Login();
        $info = $model->userLogin($where);
        
        if(!$info){
			
			//注册奖励
			$configpri=\App\getConfigPri();
			$reg_reward=$configpri['reg_reward'];
			
            /* 注册 */
            $nickname=\PhalApi\T('User').substr($user_login,-3);
			$data=array(
                'country_code'=>$country_code,
				'device_code' => $user_login,
				'user_nickname' =>$nickname,
				"source"=>$source,
				"coin"=>$reg_reward,
			);
            
            $info = $model->userReg($data);

            if($reg_reward){
            	//添加钻石记录
	            $insert=array(
	                'type'=>1,
	                'action'=>8, //注册奖励
	                'uid'=>$info['id'],
	                'touid'=>$info['id'],
	                'actionid'=>0,
	                'nums'=>0,
	                'totalcoin'=>$reg_reward,
	                'addtime'=>time()
	            );

	            \App\addCoinRecord($insert);
            }
  

        }
        
        
		if($info['user_status']=='0'){
			return 1002;
        }
		if($info['user_status']=='3'){
			return 1003;
        }
		unset($info['user_status']);
		
		$info['isreg']='0';
		$info['issexrecommend']='0';
        
		if($info['sex']==0){
			$info['isreg']='1';
			$info['issexrecommend']='1';
		}
        
        if(!$configpri['agent_switch']){
            $info['isreg']='0';
        }
        
        unset($info['last_login_time']);
 
		$info['level']=\App\getLevel($info['consumption']);
		$info['level_anchor']=\App\getLevelanchor($info['votestotal']);
		/* $info['level_anchor']=getLevelanchor($info['goodnums'],$info['badnums']); */

		$info['avatar']=\App\get_upload_path($info['avatar']);
		$info['avatar_thumb']=\App\get_upload_path($info['avatar_thumb']);

        $userinfo=$info;
		unset($userinfo['issexrecommend']);
        unset($userinfo['isreg']);
        unset($userinfo['user_status']);
        unset($userinfo['coin']);
        unset($userinfo['login_type']);
        
        \App\hMSet('userinfo_'.$userinfo['id'],$userinfo);
        
        
        $token=md5(md5($info['id'].$user_login.time()));
		$info['token']=$token;
		$model->updateToken($info['id'],$token);
        
        $usersig=\App\setSig($info['id']);
		$info['usersig']=$usersig;
        
        unset($info['goodnums']);
        unset($info['badnums']);
        
        return $info;

    }

    public function userLoginByThird($openid,$type,$nickname,$avatar,$source) {
        $rs = array();

        $configpri=\App\getConfigPri();
        $model = new Model_Login();
        
        $nowtime=time();
        
        $where=[
            'openid = ?'=>$openid,
            'login_type = ?'=>$type,
        ];
        
        $info = $model->userLogin($where);
        
        if(!$info){
            /* 注册 */
            $type_a=['web','qq','wx','sina','facebook','twitter','ios'];
			$user_login=$type_a[$type].'_'.$nowtime.rand(100,999);

			if(!$nickname){
				$nickname=\PhalApi\T('用户').substr($openid,-5);
			}
            
			if(!$avatar){
				$avatar='/default.png';
				$avatar_thumb='/default_thumb.png';
			}else{
				$avatar=htmlspecialchars_decode($avatar);
                $avatar_thumb=$avatar;
			}
			
			//注册奖励
			$configpri=\App\getConfigPri();
			$reg_reward=$configpri['reg_reward'];
            
			$data=array(
				'user_login' => $user_login,
				'user_nickname' =>$nickname,
				'avatar' =>$avatar,
				'avatar_thumb' =>$avatar_thumb,
				"source"=>$source,
				"openid"=>$openid,
				"login_type"=>$type,
				"coin"=>$reg_reward,
			);
            
            $info = $model->userReg($data);

            if($reg_reward){
            	//添加钻石记录
	            $insert=array(
	                'type'=>1,
	                'action'=>8, //注册奖励
	                'uid'=>$info['id'],
	                'touid'=>$info['id'],
	                'actionid'=>0,
	                'nums'=>0,
	                'totalcoin'=>$reg_reward,
	                'addtime'=>time()
	            );

	            \App\addCoinRecord($insert);
            }
        }
        
        
		if($info['user_status']=='0'){
			return 1002;					
		}
		if($info['user_status']=='3'){
			return 1003;
        }
		unset($info['user_status']);
		
		$info['isreg']='0';
		$info['issexrecommend']='0';
        
		if($info['sex']==0){
			$info['isreg']='1';
			$info['issexrecommend']='1';
		}
        
        if(!$configpri['agent_switch']){
            $info['isreg']='0';
        }
        
        unset($info['last_login_time']);
 
		$info['level']=\App\getLevel($info['consumption']);
        $info['level_anchor']=\App\getLevelanchor($info['votestotal']);
        /* $info['level_anchor']=getLevelanchor($info['goodnums'],$info['badnums']); */
		
		$info['avatar']=\App\get_upload_path($info['avatar']);
		$info['avatar_thumb']=\App\get_upload_path($info['avatar_thumb']);
		
		$userinfo=$info;
		unset($userinfo['issexrecommend']);
        unset($userinfo['isreg']);
        unset($userinfo['user_status']);
        unset($userinfo['coin']);
        unset($userinfo['login_type']);
        
        \App\hMSet('userinfo_'.$userinfo['id'],$userinfo);
        
        $token=md5(md5($info['id'].$user_login.time()));
		$info['token']=$token;
        $model->updateToken($info['id'],$token);
		
        $usersig=\App\setSig($info['id']);
		$info['usersig']=$usersig;
        
        unset($info['goodnums']);
        unset($info['badnums']);
        
        return $info;
    }

    public function upUserPush($uid,$pushid) {
        $rs = array();

        $model = new Model_Login();
        $rs = $model->upUserPush($uid,$pushid);

        return $rs;
    }	

	public function getCancelCondition($uid){
        $rs = array();

        $model = new Model_Login();
        $rs = $model->getCancelCondition($uid);

        return $rs;
    }

    public function cancelAccount($uid){
        $rs = array();

        $model = new Model_Login();
        $rs = $model->cancelAccount($uid);

        return $rs;
    }
	
	
	public function setUserOnline($uid,$online){
        $rs = array();

        $model = new Model_Live();
      
        /* 更新状态 */
        $model->upOnline($uid,$online);

        return $rs;
    }


}
